﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
public partial class Manage_Golbal_FlashMode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewBind();
        }
    }
    private void GridViewBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/EmbedSwfData.xml"));
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridViewBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath(@"~/XML/EmbedSwfData.xml"));
        string id = ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
        FileUpload fileupload = ((FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1"));
        string filepath = string.Empty;
        string url = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("url")).Text;
        string title = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("title")).Text;
        Random rand = new Random();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            //ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('" + ds.Tables[0].Rows[i]["id"] + "');</script>");
            if (ds.Tables[0].Rows[i]["id"].ToString().Trim() == id.Trim())
            {
                filepath = ds.Tables[0].Rows[i]["img"].ToString();
                ds.Tables[0].Rows[i]["url"] = url;
                ds.Tables[0].Rows[i]["title"] = title;
                ds.Tables[0].Rows[i]["rand"] = rand.Next(1000,9999).ToString();
                break;
            }
        }
        try
        {
            if (!string.IsNullOrEmpty(fileupload.PostedFile.FileName))
            {
                fileupload.PostedFile.SaveAs(Server.MapPath("~/") + filepath);
            }
            ds.WriteXml(Server.MapPath(@"~/XML/EmbedSwfData.xml"));
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新成功');</script>");
            GridView1.EditIndex = -1;
            GridViewBind();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新失败');</script>");
        }
    }
}